package com.metacube.metaconnect.model;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class ForgotPasswordForm extends ActionForm {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String email;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	/* 
	 * validates email_id in the forgot password form
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		
		ActionErrors errorList = new ActionErrors();
		try {
			if(email.trim().equals(""))
			{
				errorList.add("email", new ActionMessage("MetaConnect.error.email"));
				request.setAttribute("forgotPasswordError", "Please Enter Email-Id");
			}
			else if(!email.matches("^[A-Za-z][_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")){
				errorList.add("email", new ActionMessage("MetaConnect.error.invalidEmail"));
				request.setAttribute("forgotPasswordError", "Please Enter valid Email-Id");
			}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return errorList;
	}
}
